﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.EAP.DataAccess.Entity;
using Way.Bus.Model;
using Way.Bus.WMS.Controllers;
using Way.Contract.MVC;
using Way.Bus.Model.Purview;
using Way.Bus.Model.Interface;

namespace Way.Bus.WMS.Model
{
    /// <summary>
    /// 描述：出库单表
    /// 作者：柳强
    /// 创建日期:2012-12-21
    /// </summary>
    [EntityClassAttribut("Acc_WMS_OutOrder", "出库单", IsOnAppendProperty = true)]
    public class StockOutOrder : OrderCommon, IPropertyValueType
    {
        [EntityForeignKey(typeof(Organization), "ID", "OrganizationName")]
        public new string STAY5
        {
            get;
            set;
        } 

        /// <summary>
        /// 源单编码
        /// </summary>
        [EntityControl("源单编码", false, true, 2)]
        [EntityForeignKey(typeof(StockOutNotice), "CODE", "CODE")]
        [EntityField(100)]
        public string SourceCode { get; set; }
        /// <summary>
        /// 仓库
        /// </summary>
        [EntityControl("仓库", false, true, 7)]
        [EntityForeignKey(typeof(WareHouse), "ID", "WAREHOUSENAME")]
        [EntityField(255,IsNotNullable=true)]
        public int TOWHNO { get; set; }
        /// <summary>
        /// 收发货时间
        /// </summary>
        [EntityControl("收发货时间", false, true, 8)]
        [EntityField(255)]
        public  DateTime INOUTTIME { get; set; }

        /// <summary>
        /// 运输公司
        /// </summary>
        [EntityControl("运输公司", false, true, 9)]
        [EntityForeignKey(typeof(LogisticsInfo), "ID", "LOGISTICSNAME")]
        [EntityField(100)]
        public int LogCode { get; set; }

        [NotSearchAttribut]
        [EntityControl("收货人", true, false, 13)]
        public string ReceiptPerson { get; set; }

        [NotSearchAttribut]
        [EntityControl("收货人电话", true, false, 13)]
        public string ReceiptPhoneCode { get; set; }

        [NotSearchAttribut]
        [EntityControl("收货人地址", true, false, 13)]
        public string ReceiptAddress { get; set; }


        [NotSearchAttribut]
        [EntityControl("收货客户", true, false, 13)]
        public string ReceiptClient { get; set; }

        [NotSearchAttribut]
        [EntityControl("发货时间", true, false, 13)]
        public string OutTime { get; set; }

        protected override string GetSearchSQL()//增加通知单上的收货信息
        {
            StockOutNotice son = new StockOutNotice();
            string outOuder = this.ToString();
            string sql = base.GetSearchSQL();
            string nsql = " select * from (select a.*,b.ReceiptPerson,b.ReceiptPhoneCode,b.ReceiptAddress,b.ReceiptClient,b.OutTime from (" + sql + ") a "
                    + " left join (select CODE,ReceiptPerson,ReceiptPhoneCode,ReceiptAddress,ReceiptClient,OutTime from " + son.ToString() + " where IsSubmited = '1') b on b.CODE = a.SOURCECODE  ) " + outOuder;
            return nsql;
        }

        /// <summary>
        /// 出库单待出库明细
        /// </summary>
        [HierarchicalEntityControl(isadd = false, isedit = false, isremove = false, ischeck = true)]
        public HierarchicalEntityView<StockOutOrder, StockOutNoticeMaterials> StayMaterials
        {
            get
            {
                if (_StayMaterials == null)
                {
                    _StayMaterials = new HierarchicalEntityView<StockOutOrder, StockOutNoticeMaterials>(this);
                    _StayMaterials.IsAssociateInsert = false;
                    _StayMaterials.IsAssociateDelete = false;
                    _StayMaterials.IsAssociateUpdate = false;
                }
                return _StayMaterials;
            }
        }

        private HierarchicalEntityView<StockOutOrder, StockOutOrderMaterials> _Materials;
        /// <summary>
        /// 出库单出库物料集合
        /// </summary>
        public HierarchicalEntityView<StockOutOrder, StockOutOrderMaterials> Materials
        {
            get
            {
                if (_Materials == null)
                    _Materials = new HierarchicalEntityView<StockOutOrder, StockOutOrderMaterials>(this);
                return _Materials;
            }
        }



        private HierarchicalEntityView<StockOutOrder, StockOutNoticeMaterials> _StayMaterials;
       


        /// <summary>
        /// 外系统数据同步日志
        /// </summary>
        private HierarchicalEntityView<StockOutOrder, LogData> _LogItems;
        //[HierarchicalEntityControl(visible = false)]
        [HierarchicalEntityControl(isadd = false, isedit = false, isremove = false,visible=false)]
        public HierarchicalEntityView<StockOutOrder, LogData> LogItems
        {
            get
            {
                if (_LogItems == null)
                    _LogItems = new HierarchicalEntityView<StockOutOrder, LogData>(this);
                _LogItems.ChildCondition("SourceTable", "='" + this.ToString() + "'");
                return _LogItems;
            }
        }

        #region IPropertyValueType 成员

        public PropertyValueType[] GetValueType(ValueTypeArgs valueArgs)
        {
            string tn = this.ToString();
            List<PropertyValueType> list = new List<PropertyValueType>();
            if (valueArgs.ColumnName == "STOCKTYPE" && valueArgs.TableName == tn)
            {
                PropertyValueType pro = new PropertyValueType();
                pro.Text = "借货出库";
                pro.Value = "1";
                PropertyValueType pro1 = new PropertyValueType();
                pro1.Text = "发货出库";
                pro1.Value = "2";
                PropertyValueType pro2 = new PropertyValueType();
                pro2.Text = "采购出库";
                pro2.Value = "3";
                list.Add(pro);
                list.Add(pro1);
                list.Add(pro2);
            }
            if (valueArgs.ColumnName == "STATE" && valueArgs.TableName == tn)
            {
                PropertyValueType pro1 = new PropertyValueType();
                pro1.Value = "0";
                pro1.Text = "未出库";
                PropertyValueType pro2 = new PropertyValueType();
                pro2.Value = "1";
                pro2.Text = "部分出库";
                PropertyValueType pro3 = new PropertyValueType();
                pro3.Value = "2";
                pro3.Text = "出库完成";
                list.Add(pro1);
                list.Add(pro2);
                list.Add(pro3);
            }

            return list.ToArray();
        }

        #endregion
    }
}
